package com.springBoot.springBootSysCore.modules.services.system.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.springBoot.springBootSysCore.common.utils.FileUtil;
import com.springBoot.springBootSysCore.common.utils.returnUtil.CodeEnum;
import com.springBoot.springBootSysCore.common.utils.returnUtil.ResultJsonFormat;
import com.springBoot.springBootSysCore.modules.entity.system.SysFileTmp;
import com.springBoot.springBootSysCore.modules.entity.system.SysUser;
import com.springBoot.springBootSysCore.modules.entity.utils.UserUtils;
import com.springBoot.springBootSysCore.modules.repository.system.SysFileTmpRepository;
import com.springBoot.springBootSysCore.modules.repository.system.SysUserRepository;
import com.springBoot.springBootSysCore.modules.services.system.SysFileTmpService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Service("fileTmpService")
public class SysFileTmpServiceImpl implements SysFileTmpService {
	@Autowired
	private SysFileTmpRepository fileTmpRepository;
	@Autowired
	private SysUserRepository sysUserRepository;
	@Value("${init.uploadPath}")
	private String uploadPath;

	@Override
	@Transactional
	public Map<String,Object> uploadFileId(String fileId,String tmpId) {
		ResultJsonFormat rj = new  ResultJsonFormat("保存成功", CodeEnum.SUCCESS.value());
		try {
			SysFileTmp fileTmp = fileTmpRepository.findOne(tmpId);
			fileTmp.setFileId(fileId);
			fileTmp=fileTmpRepository.save(fileTmp);
			rj.setData(fileTmp);
		} catch (Exception e) {
			e.printStackTrace();
			rj.setFlag(CodeEnum.Failed.value());
			rj.setMsg("保存失败");
		}
		return rj.convertResultJson();
	}
	@Override
	public List<SysFileTmp> findAll() {
		return fileTmpRepository.findAll();
	}
	@Override
	public void delete(SysFileTmp fileTmp) {
		if(fileTmp!=null){
			fileTmpRepository.delete(fileTmp);
		}
	}
	@Override
	@Transactional
	public Map<String, Object> uploadFile(MultipartFile file,String objId) {
		ResultJsonFormat rj = new  ResultJsonFormat("保存成功", CodeEnum.SUCCESS.value());
		try {
	        String userAccount = UserUtils.getUser().getAccount();
	        SysFileTmp fileTmp = FileUtil.springAjax_oneToTemp(file, uploadPath + "\\" + userAccount, null, new ArrayList<String>(), 0);
	        fileTmp.setDataPath(fileTmp.getPath().replace("\\", "/").substring(uploadPath.length()));
	        fileTmp.setObjId(objId);
	        rj.setData(fileTmp);
	        fileTmpRepository.save(fileTmp);
		} catch (Exception e) {
			e.printStackTrace();
			rj.setFlag(CodeEnum.Failed.value());
			rj.setMsg("保存失败");
		}
        return rj.convertResultJsonForData();
	}
	@Override
	@Transactional
	public Map<String, Object> deleteFile(String fileId, String objId) {
		ResultJsonFormat rj = new  ResultJsonFormat("保存成功", CodeEnum.SUCCESS.value());
		try {
			SysFileTmp fileTmp=fileTmpRepository.queryByCondition(fileId, objId);
	        fileTmpRepository.deleteFile(fileId, objId);
	        FileUtil.deleteFile(fileTmp.getPath());
		} catch (Exception e) {
			e.printStackTrace();
			rj.setFlag(CodeEnum.Failed.value());
			rj.setMsg("保存失败");
		}
        return rj.convertResultJsonForData();
	}

	@Override
	@Transactional
	public Map<String, Object> uploadFile(MultipartFile[] files, String username, String clientDigest) {
		ResultJsonFormat rj = new  ResultJsonFormat("保存成功", CodeEnum.SUCCESS.value());
		try {
			if(clientDigest.equals(sysUserRepository.findByAccount(username).getClientDigest())){
				List<String> ids = new ArrayList<>();
				for(MultipartFile file: files){
					SysFileTmp fileTmp = FileUtil.springAjax_oneToTemp(file, uploadPath + "\\" + username, null, new ArrayList<String>(), 0);
					fileTmp.setDataPath(fileTmp.getPath().replace("\\", "/").substring(uploadPath.length()));
					fileTmp=fileTmpRepository.save(fileTmp);
					ids.add(fileTmp.getId());
				}
				rj.setData(ids);
			}
		} catch (Exception e) {
			e.printStackTrace();
			rj.setFlag(CodeEnum.Failed.value());
			rj.setMsg("保存失败");
		}
		return rj.convertResultJsonForData();
	}

	@Override
	@Transactional
	public String getPath(String[] fileTmpId) {
		List<String> ids = Arrays.asList(fileTmpId);
		if(ids.size()<=0){
			return "";
		}
		List<SysFileTmp> ls =  fileTmpRepository.findByIds(ids);
		StringBuffer urls = new StringBuffer("");
		for(int i = 0; i<ls.size();i++){
			urls.append(ls.get(i).getDataPath());
			if(i!=ls.size()-1){
				urls.append(",");
			}
		}
		fileTmpRepository.updateFilteDel(ids);
		return urls.toString();
	}

	@Override
	@Transactional
	public Map<String, Object> fileupload(HttpServletRequest httpServletRequest, String username, String clientDigest) {
		ResultJsonFormat rj = new  ResultJsonFormat("保存成功", CodeEnum.SUCCESS.value());
		try {
			if(clientDigest.equals(sysUserRepository.findByAccount(username).getClientDigest())){
//				List<String> ids = new ArrayList<>();
				StringBuffer ids =new StringBuffer();
				Map<String, String> m = FileUtil.upLoadFile("userfiles/app", httpServletRequest);
				if(StringUtils.isNotBlank(m.get("file"))){
					SysFileTmp fileTmp = new SysFileTmp();
					fileTmp.setDataPath(m.get("file"));
					fileTmp=fileTmpRepository.save(fileTmp);
//					ids.add(fileTmp.getId());
					ids.append(fileTmp.getId());
				}
//				for(MultipartFile file: files){
//					SysFileTmp fileTmp = FileUtil.springAjax_oneToTemp(file, uploadPath + "\\" + username, null, new ArrayList<String>(), 0);
//					fileTmp.setDataPath(fileTmp.getPath().replace("\\", "/").substring(uploadPath.length()));
//					fileTmp=fileTmpRepository.save(fileTmp);
//					ids.add(fileTmp.getId());
//				}
				rj.setData(ids);
			}
		} catch (Exception e) {
			e.printStackTrace();
			rj.setFlag(CodeEnum.Failed.value());
			rj.setMsg("保存失败");
		}
		return rj.convertResultJsonForData();
	}

}
